package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.FlowerMapper;
import com.wjh.pojo.Flower;
import com.wjh.service.FlowerService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 花的种类(Flower)表服务实现类
 *
 * @author makejava
 * @since 2023-10-16 19:22:08
 */
@Service("flowerService")
public class FlowerServiceImpl extends ServiceImpl<FlowerMapper, Flower> implements FlowerService {


    @Override
    public ResponseResult flowerList(Integer pageNum, Integer pageSize, Integer type, String flowerName) {
        LambdaQueryWrapper<Flower> queryWrapper = new LambdaQueryWrapper<Flower>();
        queryWrapper.eq(type != null,Flower::getType,type);
        queryWrapper.like(flowerName != null,Flower::getName,flowerName);
        //分页查询所有花
        Page<Flower> certificatePage = new Page<>(pageNum,pageSize);
        Page<Flower> page = page(certificatePage, queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public ResponseResult delByIds(List<Integer> id) {
        //根据id删除数据
        LambdaQueryWrapper<Flower> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.in(Flower::getId,id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addFlower(Flower flower) {
        if(!StringUtils.hasText(flower.getName())){
            throw new SystemException(AppHttpCodeEnum.NEED_NAME);
        }
        if(!StringUtils.hasText(flower.getPrice())){
            throw new SystemException(AppHttpCodeEnum.NEED_PRICE);
        }
        if(!StringUtils.hasText(flower.getImage())){
            throw new SystemException(AppHttpCodeEnum.NEED_IMAGE);
        }
        if(flower.getType() == null){
            throw new SystemException(AppHttpCodeEnum.NEED_TYPE);
        }
        save(flower);
        return ResponseResult.okResult();
    }

    @Override
    public void delByType(Integer typeId) {
        LambdaQueryWrapper<Flower> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Flower::getType,typeId);
        remove(queryWrapper);
    }

    @Override
    public ResponseResult queryTypeFlower(Integer floristId, Integer typeId) {
        FlowerMapper baseMapper = getBaseMapper();
        List<Flower> flowers = baseMapper.queryTypeFlower(floristId, typeId);
        return ResponseResult.okResult(new PageVo(flowers, (long) flowers.size()));
    }

    @Override
    public ResponseResult likeFlower(String name) {
        if(!StringUtils.hasText(name)){
            throw new SystemException(AppHttpCodeEnum.NEED_CONTENT);
        }
        LambdaQueryWrapper<Flower> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.like(Flower::getName,name);
        List<Flower> list = list(queryWrapper);
        return ResponseResult.okResult(new PageVo(list,(long)list.size()));
    }
}
